package com.bd.scm.module.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.domain.dto.user.UserDto;
import com.bd.scm.domain.models.user.DtFace;
import com.bd.scm.domain.models.user.DtUser;
import com.bd.scm.domain.models.user.UserInfo;
import com.bd.scm.module.enums.ExceptionEmun;
import com.bd.scm.module.login.service.LoginService;
import com.bd.scm.module.user.service.IFaceService;
import com.bd.scm.module.user.service.UserInfoService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@Api("员工人脸相关接口")
@RestController
@RequestMapping("/api/user/face")
public class FactController {

    @Value("${bd.thread.enable}")
    private boolean flag;

    @Autowired
    private IFaceService iFaceService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private LoginService loginService;

    @PostMapping
    public Result add(@RequestBody DtFace face, HttpServletRequest request) {
        int iResult = iFaceService.add(face);
        return iResult > 0 ? ResultUtil.ok() : ResultUtil.error(ExceptionEmun.SERVICE_ERROR_ADD);
    }

    @PostMapping("/photo")
    public Result addUserWithFace(HttpServletRequest request, String userData) throws IOException {

        String clientIp = request.getRemoteAddr();

        PersonnelFilesDto useDto = JSON.parseObject(userData, PersonnelFilesDto.class);

        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");

        DtUser user = iFaceService.addUserWithFace(files, useDto, clientIp);

        return ResultUtil.ok(user);
    }


    @PostMapping("/getUserInfoByUserNo")
    public Result getUserInfoByUserNo(@RequestBody UserDto userDto) throws IOException {
        System.err.println(userDto.toString());
        UserInfo userInfo = userInfoService.getUserInfoByUserNo(userDto);
        return ResultUtil.ok(userInfo);
    }

    @PostMapping("/takePhoto")
    public Result takePhoto(@RequestParam("file") MultipartFile file, @RequestParam("userInfo") String userInfoStr) throws IOException {
        UserInfo userInfo = JSON.parseObject(userInfoStr, UserInfo.class);
        Boolean flag = iFaceService.takePhoto(file, userInfo);
        return ResultUtil.ok(flag);
    }

    @PostMapping("/photo2")
    public Result addUserWithFace2(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        Result result = null;
        String clientIp = request.getRemoteAddr();
        if(flag){
            JSONObject user = (JSONObject) request.getSession().getAttribute("user");
            if(user!=null){
                String operator = user.getString("userSerial");
                int userFace = loginService.getUserFace(operator);
                if(2 == userFace){
                    result = ResultUtil.error("已存在人脸");
                }else{
                    result = ResultUtil.ok(iFaceService.addFace(file, clientIp,operator));
                }
            }else {
                result.setSuccess(false);
                result.setMessage("登陆超时，请重新登录!");
            }
        }else{
            String operator = "20000002";
            result = ResultUtil.ok(iFaceService.addFace(file, clientIp,operator));
        }
        return result;
    }

    /**
     * 图片回显返回base64编码 lengnianguo 20240129
     * @param userId
     * @param request
     * @param response
     * @return
     */
    @GetMapping("responseFileStream")
    public Result responseFileStream(String userId, HttpServletRequest request, HttpServletResponse response){
        Result result = null;
        result = Result.ok(iFaceService.responseFileStream(userId,response));
        return result;
    }
}
